package
{
	import flash.display.GradientType;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BevelFilter;
	import flash.geom.Vector3D;
	import flash.text.TextField;
	import flash.geom.ColorTransform;
	
	public class FlowBall extends Sprite
	{
		public function FlowBall()
		{
			var con:MovieClip = new MovieClip();
			addChild(con);
			con.x = 275;
			con.y = 200;
			con.z = 0;
			var arr:Array = [];
			for (var i:int; i < 12; i++)
			{
				var mc1:MovieClip = ball(6, 0x00ffff);
				var mc2:MovieClip = ball(10, 0x333300);
				var mc3:MovieClip = ball(6, 0x00ffff);
				mc1.x = mc2.x = mc3.x = 110 * Math.cos(i * 30 * Math.PI / 180);
				mc1.y = mc2.y = mc3.y = 110 * Math.cos(i * 30 * Math.PI / 180);
				mc1.z = 16;
				mc2.z = 0;
				mc3.z = -16;
				con.addChild(mc1);
				con.addChild(mc2);
				con.addChild(mc3);
				arr.push(mc1, mc2, mc3);
			}
			var shizhen:MovieClip = line(5, 5, -70, 0x9900ff);
			var fenzhen:MovieClip = line(3, 5, -80, 0x9900ff);
			var miaozhen:MovieClip = line(2, 10, -90, 0xff0000);
			shizhen.z = 2;
			fenzhen.z = 0;
			miaozhen.z = 2;
			var sp:MovieClip = new MovieClip();
			con.addChild(sp);
			sp.z = 2;
			var kedu:MovieClip = new MovieClip();
			sp.addChild(kedu);
			kedu.z = 3;
			sp.addChild(shizhen);
			sp.addChild(fenzhen);
			sp.addChild(miaozhen);
			sp.filters = [new BevelFilter(2, 45, 0xffffff, 1, 0xffffff)];
			for (i = 0; i < 60; i++)
			{
				var kd:MovieClip = line(i % 3 == 0 ? 2 : 1, -93, -96, 0x666666);
				kedu.addChild(kd);
				kd.rotation = i * 6;
			}
			// 添加文字
			for (i=1; i<=12; i++) 
			{
				var txt:TextField =new TextField();
				txt.text=""+i;
				txt.textColor=0x666666;
				kedu.addChild(txt);
				txt.autoSize="left";
				txt.x=85*Math.cos(i*30*Math.PI/180-Math.PI/2)-txt.width/2;
				txt.y=85*Math.sin (i*30*Math.PI /180-Math.PI/2)-txt.height /2;
			}
			var arrA:Array=[shizhen,miaozhen,fenzhen,kedu];
			arr.push(sp);
			addEventListener(Event.ENTER_FRAME ,frame);
			
			function frame(e):void 
			{
				var mydate:Date =new Date();
				var h:Number=mydate.hours;
				var m:Number=mydate.minutes;
				var s:Number=mydate.seconds;
				shizhen.rotation=h*30+m*0.5;
				fenzhen.rotation=m*6+s*0.1;
				miaozhen.rotation=s*6;
				var ang:Number =(mouseX-275)/10;
				
				for (i=0; i<arr.length; i++) 
				{
					arr[i].transform.matrix3D.appendRotation(ang,Vector3D.Y_AXIS);
					arr[i].transform.matrix3D.prependRotation(-ang,Vector3D.Y_AXIS);
				}
				
				sp.transform.matrix3D.prependRotation(ang,Vector3D.Y_AXIS);
				arr.sortOn("z",18);
				for (i=0; i<arr.length; i++) 
				{
					con.setChildIndex(arr[i],i);
					arr[i].transform.colorTransform=new ColorTransform(1,1,1,1,-arr[i].z,-arr[i].z);
				}
				for (i=0; i<arrA.length; i++) 
				{
					if (sp.z>0)
					{
						sp.setChildIndex(arrA[i],i);
					} 
					else 
					{
						sp.setChildIndex(arrA[i],arrA.length -1-i);
					}
				}
				con.z=-mouseY/400*150;
			}
		}
		
		private function ball(r:int, color:uint):MovieClip
		{
			var mc:MovieClip = new MovieClip();
			mc.graphics.beginGradientFill(GradientType.RADIAL, [color, 0xfffff], [1, 1], [0, 255]);
			mc.graphics.drawCircle(0, 0, r);
			
			return mc;
		}
		
		private function line(k:int, a:Number, b:Number, color:uint):MovieClip
		{
			var mc:MovieClip = new MovieClip();
			mc.graphics.lineStyle(k, color);
			mc.graphics.moveTo(0, a);
			mc.graphics.lineTo(0, b);
			mc.graphics.lineStyle(1, 0x000000);
			mc.graphics.drawCircle(0, 0, 2);
			
			return mc;
		}
	}
}